<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/24
 * Time: 10:07
 */

namespace app\Service;


use app\api\model\Index\VideoUserComment;

class CommentsService extends BaseService
{
    protected $table;

    public function __construct()
    {
        $this->table = new VideoUserComment();
    }

    /**
     * @param int $video_id
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 评论列表
     */
    public function lists(int $video_id, int $page, int $limit)
    {
        $offset = ($page - 1) * $limit;

        $data = $this->table
            ->with(['user' => function ($query) {
                return $query->field('id,head_img,nickname');
            }, 'video' => function ($query) {
                return $query->field('id,user_id');
            }])
            ->when($video_id, function ($query) use ($video_id) {
                return $query->where('video_id', '=', $video_id);
            })
            ->where('is_del', '=', 0)
            ->order(['top' => 'desc', 'add_time' => 'desc'])
            ->limit($offset, $limit)
            ->select()->map(function ($model) {
                return [
                    'id' => $model['id'],
                    'content' => $model['content'],
                    'time' => $model['add_time'],
                    'video_id' => $model['video_id'],
                    'pid' => $model['pid'],
                    'num_video_comment_like' => $model['num'],
                    'is_top' => $model['top'],
                    'user_avatar' => $model['user']['head_img'],
                    'user_nickname' => $model['user']['nickname'],
                    'user_id' => $model['user']['id'],
                    'is_author' => $this->is_author($model['user']['id'], $model['video']['user_id']),
                ];
            });
        $data =$this->arraySort($data,'is_author');
        return $this->getTree($data);
    }

    /**
     * @param $array
     * @param $keys
     * @param string $sort
     * @return array
     * 排序
     */
    function arraySort($array, $keys, $sort = 'desc')
    {
        $newArr = $valArr = array();
        foreach ($array as $key => $value) {
            $valArr[$key] = $value[$keys];
        }
        ($sort == 'asc') ? asort($valArr) : arsort($valArr);
        reset($valArr);
        foreach ($valArr as $key => $value) {
            $newArr[$key] = $array[$key];
        }
        return $newArr;
    }

    /**
     * @param $lists
     * @param string $key_name
     * @param string $p_name
     * @param int $p_start
     * @return array
     */
    function getTree($lists, $key_name = 'id', $p_name = 'pid', $p_start = 0)
    {
        $tree = [];
        foreach ($lists as $list) {
            if ($list[$p_name] == $p_start) {
                $row = $list;
                $children = $this->getTree($lists, $key_name, $p_name, $row[$key_name]);
                if ($children) {
                    $row['children'] = $children;
                }
                $row['children'] = null;
                $tree[] = $row;
            }
        }

        return $tree;
    }

    /**
     * @param int $user_id
     * @param int $uuid
     * @return bool
     * 是否作者
     */
    public function is_author(int $user_id, int $uuid): bool
    {
        if ($user_id == $uuid) {
            return true;
        }
        return false;
    }

    /**
     * @param $video_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 查询一条数据
     */
    public function first(int $id)
    {
        $re = $this->table
            ->with(['user' => function ($query) {
                return $query->field('id,head_img,nickname');
            }, 'video' => function ($query) {
                return $query->field('id,user_id');
            }])
            ->where('id', $id)
            ->find()->map(function ($model) {
                return [
                    'id' => $model['id'],
                    'content' => $model['content'],
                    'time' => $model['add_time'],
                    'video_id' => $model['video_id'],
                    'pid' => $model['pid'],
                    'num_video_comment_like' => $model['num'],
                    'is_top' => $model['top'],
                    'user_avatar' => $model['user']['head_img'],
                    'user_nickname' => $model['user']['nickname'],
                    'user_id' => $model['user']['id'],
                    'is_author' => $this->is_author($model['user']['id'], $model['video']['user_id'])
                ];
            });
        return $re;
    }
}